﻿using Anno.DynamicApi;
using Anno.EngineData;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Web;

namespace Anno.DynamicApi
{
    [Anno.Loader.NotInInject]
    /// <summary>
    /// Json返回
    /// </summary>
    public class ApiResult: ActionResult
    {
        /// <summary>
        /// 结果编码
        /// </summary>
        //[Required]
       // public virtual int Code { get; set; } = 0;

        /// <summary>
        /// 接口错误信息
        /// </summary>
        //public virtual string Msg { get; set; } = "";
        /// <summary>
        /// 
        /// </summary>
        //public int Unpack { get; set; } = 1;
        //-------------------------------------------------------------------------------------
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ApiResult() { }
        public ApiResult(object _data)
        {
            this.Status = true;
            OutputData = _data;
        }

        /// <summary>
        /// 接口失败返回数据
        /// </summary>
        public ApiResult(string errorMsg, ResultCode resultCode)
        {
            Msg = errorMsg;
            Status = resultCode == ResultCode.OK;
        }
        //-------------------------------------------------------------------------------------
        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name = "_data" ></ param >
        /// < returns ></ returns >
        public static ApiResult Success()
        {
            var rets = new ApiResult();
            return rets;
        }
        /// <summary>
        /// 返回异常
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static ApiResult Exception(Exception ex)
        {
            return Faild(ex.GetExceptionMessage());
        }
        /// <summary>
        /// 返回调用失败
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ApiResult Faild(string message)
        {
            var rets = new ApiResult() { Status = false, Msg = message };
            return rets;
        }
        //-------------------------------------------------------------------------------------
        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name = "_data" ></ param >
        /// < returns ></ returns >
        public static ApiResult Success<TData>(TData _data)
        {
            return new ApiResult(_data);
        }
        /// <summary>
        /// 返回异常
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static ApiResult Exception<TData>(Exception ex, TData _data = default(TData))
        {
            var rets = new ApiResult() { OutputData = _data, Status = false, Msg = ex?.ToString() };
            return rets;
        }
        /// <summary>
        /// 返回调用失败
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ApiResult Faild<TData>(string message, TData _data = default(TData))
        {
            var rets = new ApiResult() { OutputData = _data, Status = false, Msg = message };
            return rets;
        }
        //-------------------------------------------------------------------------------------
        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name = "_data" ></ param >
        /// < returns ></ returns >
        public static ApiResult Success<TData>(List<TData> _data)
        {
            return new ApiResult(_data);
        }
        /// <summary>
        /// 返回异常
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static ApiResult Exception<TData>(Exception ex, List<TData> _data = null)
        {
            var rets = new ApiResult() { OutputData = _data, Status = false, Msg = ex?.ToString() };
            return rets;
        }
        /// <summary>
        /// 返回调用失败
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ApiResult Faild<TData>(string message, List<TData> _data = null)
        {
            var rets = new ApiResult() { OutputData = _data,Status = false, Msg = message };
            return rets;
        }
        ///// <summary>
        ///// 返回成功
        ///// </summary>
        ///// <param name = "_data" ></ param >
        ///// < returns ></ returns >
        //public static ApiListResult<TData> Success<TData>(List<TData> _data)
        //{
        //    return new ApiListResult<TData>(_data);
        //}
        //-------------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="IsCamelCaseNames">返回结果是否为驼峰命名</param>
        /// <returns></returns>
        public System.Net.Http.HttpResponseMessage ToJson(bool IsCamelCaseNames = false)
        {
            return  ApiHelper.ToJson(this, IsCamelCaseNames);
        }
    }

    //[Anno.Loader.NotInInject]
    ///// <summary>
    ///// Json返回
    ///// </summary>
    //public class ApiResult<TData> : ActionResult<TData>
    //{
    //    /// <summary>
    //    /// 数据
    //    /// </summary>
    //    //public TData Data { get; set; }

    //    /// <summary>
    //    /// 默认构造函数
    //    /// </summary>
    //    public ApiResult() : base() { }
    //    /// <summary>
    //    /// 接口成功返回数据
    //    /// </summary>
    //    public ApiResult(TData data)
    //    {
    //        this.Status = true;// ResultCode.OK.GetHashCode();
    //        this.OutputData = data;
    //    }
    //}

    ///// <summary>
    ///// Json返回
    ///// </summary>
    //public class ApiListResult<TData> :ApiResult
    //{
    //    /// <summary>
    //    /// 数据
    //    /// </summary>
    //    public List<TData> Data { get; set; }
    //    /// <summary>
    //    /// 默认构造函数
    //    /// </summary>
    //    public ApiListResult() : base() { }
    //    /// <summary>
    //    /// 接口成功返回数据
    //    /// </summary>
    //    /// <param name="data"></param>
    //    public ApiListResult(List<TData> data) : this()
    //    {
    //        this.Code = ResultCode.OK.GetHashCode();
    //        this.Data = data; 
    //    }
    //}

}
